package com.company.cloud.user.modules.app.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import com.company.cloud.captcha.modules.api.model.CaptchaVO;
import com.company.common.server.config.cloud.ProjectConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.captcha.modules.api.model.Captcha;
import com.company.cloud.captcha.modules.service.CaptchaService;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.HttpContextUtils;
import com.company.cloud.user.constant.UserConstant;
import com.company.cloud.user.constant.UserMsgSendTypeEnum;
import com.company.cloud.user.modules.app.service.UserCodeService;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("/app/user/vcode")
@Api(tags = "User_App_用户验证码服务")
public class UserCodeController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserCodeService userCodeService;

    @Autowired
    private CaptchaService captchaService;

    @Autowired
    private ProjectConfig projectConfig;

    @PostMapping("/sendLoginVCode")
    @ApiOperation(value = "发送登录验证码", response = BaseResult.class)
    public BaseResult sendLoginVCodeByCompany(@RequestBody Captcha captcha) {
        captchaService.verification(UserConvertUtils.getCaptcha2VO(captcha));
        String companyNo = projectConfig.getCompanyNo(AuthContextUtils.getCompanyNo());
        userCodeService.sendCodeByCompany(companyNo, UserMsgSendTypeEnum.Login_VCode, captcha.getMobile());
        return BaseResult.ok();
    }

    @PostMapping("/sendRegVCode")
    @ApiOperation(value = "发送注册验证码", response = BaseResult.class)
    public BaseResult sendRegVCode(@RequestBody Captcha captcha) {
//        captchaService.verification(UserConvertUtils.getCaptcha2VO(captcha));
        String companyNo = projectConfig.getCompanyNo(AuthContextUtils.getCompanyNo());
        userCodeService.sendCodeByCompany(companyNo, UserMsgSendTypeEnum.Reg_VCode, captcha.getMobile());
        return BaseResult.ok();
    }

    @PostMapping("/sendBindVCode")
    @ApiOperation(value = "发送三方绑定验证码", response = BaseResult.class)
    public BaseResult sendBindVCode(@RequestBody Captcha captcha) {
//        captchaService.verification(UserConvertUtils.getCaptcha2VO(captcha));
        String companyNo = projectConfig.getCompanyNo(AuthContextUtils.getCompanyNo());
        userCodeService.sendCodeByCompany(companyNo, UserMsgSendTypeEnum.Third_Bind_VCode, captcha.getMobile());
        return BaseResult.ok();
    }

    @PostMapping("/sendCaptcha")
    @ApiOperation(value = "发送登录验证码", response = BaseResult.class)
    public BaseResult sendCaptcha(@RequestBody Captcha captcha) {
        String companyNo = projectConfig.getCompanyNo(AuthContextUtils.getCompanyNo());
        userCodeService.sendCodeByCompany(companyNo, UserMsgSendTypeEnum.Login_VCode, captcha.getMobile());
        return BaseResult.ok();
    }


}
